home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / Onboard / utils.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  9.0 KB  |  253 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. from xml.dom import minidom
  6. from copy import deepcopy
  7. from KeyGtk import *
  8. import KeyCommon
  9. INSTALL_DIR = '/usr/share/onboard'
  10. modifiers = {
  11.     'shift': 1,
  12.     'caps': 2,
  13.     'control': 4,
  14.     'mod1': 8,
  15.     'mod2': 16,
  16.     'mod3': 32,
  17.     'mod4': 64,
  18.     'mod5': 128 }
  19. modDic = {
  20.     'LWIN': ('Win', 64),
  21.     'RTSH': ('\xe2\x87\xa7'.decode('utf-8'), 1),
  22.     'LFSH': ('\xe2\x87\xa7'.decode('utf-8'), 1),
  23.     'RALT': ('Alt Gr', 128),
  24.     'LALT': ('Alt', 8),
  25.     'RCTL': ('Ctrl', 4),
  26.     'LCTL': ('Ctrl', 4),
  27.     'CAPS': ('CAPS', 2),
  28.     'NMLK': ('Nm\nLk', 16) }
  29. otherDic = {
  30.     'RWIN': 'Win',
  31.     'MENU': 'Menu',
  32.     'BKSP': '\xe2\x87\xa6'.decode('utf-8'),
  33.     'RTRN': 'Return',
  34.     'TAB': 'Tab',
  35.     'INS': 'Ins',
  36.     'HOME': 'Hm',
  37.     'PGUP': 'Pg\nUp',
  38.     'DELE': 'Del',
  39.     'END': 'End',
  40.     'PGDN': 'Pg\nDn',
  41.     'UP': '\xe2\x86\x91'.decode('utf-8'),
  42.     'DOWN': '\xe2\x86\x93'.decode('utf-8'),
  43.     'LEFT': '\xe2\x86\x90'.decode('utf-8'),
  44.     'RGHT': '\xe2\x86\x92'.decode('utf-8'),
  45.     'KP0': '0',
  46.     'KP1': '1',
  47.     'KP2': '2',
  48.     'KP3': '3',
  49.     'KP4': '4',
  50.     'KP5': '5',
  51.     'KP6': '6',
  52.     'KP7': '7',
  53.     'KP8': '8',
  54.     'KP9': '9',
  55.     'KPDL': 'Del',
  56.     'KPEN': 'Ent' }
  57. funcKeys = (('ESC', 65307), ('F1', 65470), ('F2', 65471), ('F3', 65472), ('F4', 65473), ('F5', 65474), ('F6', 65475), ('F7', 65476), ('F8', 65477), ('F9', 65478), ('F10', 65479), ('F11', 65480), ('F12', 65481), ('Prnt', 65377), ('Scroll', 65300), ('Pause', 65299))
  58. keysyms = {
  59.     'space': 65408,
  60.     'insert': 65438,
  61.     'home': 65360,
  62.     'page_up': 65365,
  63.     'page_down': 65366,
  64.     'end': 65367,
  65.     'delete': 65439,
  66.     'return': 65293,
  67.     'backspace': 65288 }
  68.  
  69. def run_script(script, sok):
  70.     a = __import__(script)
  71.     a.run(sok)
  72.  
  73.  
  74. def get_install_dir():
  75.     thisFilePath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  76.     if os.path.isfile(os.path.join(thisFilePath, 'data', 'onboard.svg')):
  77.         return thisFilePath
  78.     if os.path.isdir(INSTALL_DIR):
  79.         return INSTALL_DIR
  80.  
  81.  
  82. def create_layout_XML(name, vk, sok):
  83.     '''Reads layout stored within onBoard and outputs it to XML'''
  84.     doc = minidom.Document()
  85.     keyboard_element = doc.createElement('keyboard')
  86.     keyboard_element.setAttribute('id', name)
  87.     doc.appendChild(keyboard_element)
  88.     f = open(os.path.join(sok.SOK_INSTALL_DIR, 'layouts', 'template.svg'))
  89.     baseDoc = minidom.parse(f)
  90.     f.close()
  91.     paneDocs = []
  92.     for pane in sok.keyboard.panes:
  93.         paneDoc = deepcopy(baseDoc)
  94.         paneDocs.append(paneDoc)
  95.     
  96.     _create_pane_xml(sok.keyboard.basePane, doc, baseDoc, vk, name)
  97.     for i in range(len(paneDocs)):
  98.         _create_pane_xml(sok.keyboard.panes[i], doc, paneDocs[i], vk, name)
  99.     
  100.     docFile = open(os.path.join(os.path.expanduser('~'), '.sok', 'layouts', '%s.sok' % name), 'w')
  101.     docFile.write(doc.toxml())
  102.     docFile.close()
  103.     docFile = open(os.path.join(os.path.expanduser('~'), '.sok', 'layouts', '%s-%s.svg' % (name, sok.keyboard.basePane.ident)), 'w')
  104.     docFile.write(baseDoc.toxml())
  105.     docFile.close()
  106.     for i in range(len(paneDocs)):
  107.         docFile = open(os.path.join(os.path.expanduser('~'), '.sok', 'layouts', '%s-%s.svg' % (name, sok.keyboard.panes[i].ident)), 'w')
  108.         docFile.write(paneDocs[i].toxml())
  109.         docFile.close()
  110.     
  111.  
  112.  
  113. def _create_pane_xml(pane, doc, svgDoc, vk, name):
  114.     '''
  115.     @type   pane: Onboard.Pane.Pane
  116.     @param  pane: Pane object that we are creating xml for.
  117.  
  118.     @type   doc: xml.dom.minidom.Document
  119.     @param  doc: DOM of .sok layout file.
  120.  
  121.     @type   svgDoc: xml.dom.minidom.Document.
  122.     @param  svgDoc: DOM of this panes SVG file.
  123.  
  124.     @type   vk:     Virtkey.Virtkey
  125.  
  126.     @type   name:   str
  127.     @param  name:   Name of layout to be created.
  128.  
  129.     '''
  130.     config_element = _make_pane_config_xml(doc, pane.ident, '%s-%s.svg' % (name, pane.ident), pane.rgba, pane.fontSize)
  131.     doc.documentElement.appendChild(config_element)
  132.     svgDoc.documentElement.setAttribute('width', str(pane.viewPortSizeX))
  133.     svgDoc.documentElement.setAttribute('height', str(pane.viewPortSizeY))
  134.     for keyKey, keyVal in pane.keys.items():
  135.         if keyVal.__class__ == RectKey:
  136.             svgDoc.documentElement.appendChild(make_xml_rect(doc, keyKey, keyVal.x, keyVal.y, keyVal.width, keyVal.height, keyVal.rgba))
  137.             config_element.appendChild(_make_key_xml(doc, keyKey, keyVal))
  138.             continue
  139.         if keyVal.__class__ == LineKey:
  140.             print 'funky keys not yet implemented'
  141.             continue
  142.     
  143.  
  144.  
  145. def _make_pane_config_xml(doc, ident, filename, rgba, font):
  146.     pane_element = doc.createElement('pane')
  147.     pane_element.setAttribute('id', ident)
  148.     pane_element.setAttribute('filename', filename)
  149.     pane_element.setAttribute('backgroundRed', str(rgba[0]))
  150.     pane_element.setAttribute('backgroundGreen', str(rgba[1]))
  151.     pane_element.setAttribute('backgroundBlue', str(rgba[2]))
  152.     pane_element.setAttribute('backgroundAlpha', str(rgba[3]))
  153.     pane_element.setAttribute('font', str(font))
  154.     return pane_element
  155.  
  156.  
  157. def make_xml_rect(doc, ident, x, y, width, height, rgba):
  158.     rect_element = doc.createElement('rect')
  159.     rect_element.setAttribute('id', ident)
  160.     rect_element.setAttribute('x', str(x))
  161.     rect_element.setAttribute('y', str(y))
  162.     rect_element.setAttribute('width', str(width))
  163.     rect_element.setAttribute('height', str(height))
  164.     rect_element.setAttribute('style', 'fill:#%s%s%s;stroke:#000000;' % (dec_to_hex_colour(rgba[0]), dec_to_hex_colour(rgba[1]), dec_to_hex_colour(rgba[2])))
  165.     return rect_element
  166.  
  167.  
  168. def dec_to_hex_colour(dec):
  169.     hexString = hex(int(255 * dec))[2:]
  170.     if len(hexString) == 1:
  171.         hexString = '0' + hexString
  172.     
  173.     return hexString
  174.  
  175.  
  176. def _make_key_xml(doc, ident, key):
  177.     key_element = doc.createElement('key')
  178.     if ident in otherDic:
  179.         key_element.setAttribute('label', otherDic[ident])
  180.     
  181.     if key.action_type != KeyCommon.KEYCODE_ACTION:
  182.         if key.labels:
  183.             if key.labels[0]:
  184.                 key_element.setAttribute('label', key.labels[0])
  185.             
  186.             if key.labels[1]:
  187.                 key_element.setAttribute('cap_label', key.labels[1])
  188.             
  189.             if key.labels[2]:
  190.                 key_element.setAttribute('shift_label', key.labels[2])
  191.             
  192.             if key.labels[3]:
  193.                 key_element.setAttribute('altgr_label', key.labels[3])
  194.             
  195.             if key.labels[4]:
  196.                 key_element.setAttribute('altgrNshift_label', key.labels[4])
  197.             
  198.         
  199.     
  200.     key_element.setAttribute('id', ident)
  201.     if key.action_type == KeyCommon.CHAR_ACTION:
  202.         key_element.setAttribute('char', key.action)
  203.     elif key.action_type == KeyCommon.KEYSYM_ACTION:
  204.         key_element.setAttribute('keysym', str(key.action))
  205.     elif key.action_type == KeyCommon.KEYCODE_ACTION:
  206.         key_element.setAttribute('keycode', str(key.action))
  207.     elif key.action_type == KeyCommon.MODIFIER_ACTION:
  208.         for k, val in modifiers.items():
  209.             if key.action == val:
  210.                 key_element.setAttribute('modifier', k)
  211.                 continue
  212.         
  213.     elif key.action_type == KeyCommon.MACRO_ACTION:
  214.         key_element.setAttribute('macro', str(key.action))
  215.     elif key.action_type == KeyCommon.SCRIPT_ACTION:
  216.         key_element.setAttribute('script', key.action)
  217.     
  218.     if key.fontOffsetX:
  219.         key_element.setAttribute('font_offset_x', key.fontOffsetX)
  220.     
  221.     if key.fontOffsetY:
  222.         key_element.setAttribute('font_offset_y', key.fontOffsetY)
  223.     
  224.     if key.sticky:
  225.         key_element.setAttribute('sticky', 'true')
  226.     else:
  227.         key_element.setAttribute('sticky', 'false')
  228.     return key_element
  229.  
  230.  
  231. def matmult(m, v):
  232.     ''' Matrix-vector multiplication '''
  233.     nrows = len(m)
  234.     w = [
  235.         None] * nrows
  236.     for row in range(nrows):
  237.         w[row] = reduce((lambda x, y: x + y), map((lambda x, y: x * y), m[row], v))
  238.     
  239.     return w
  240.  
  241. if __name__ == '__main__':
  242.     from sys import argv
  243.     if argv[0]:
  244.         from virtkey import virtkey
  245.         from sok import Sok
  246.         s = Sok()
  247.         vk = virtkey()
  248.         create_layout_XML(argv[0], vk, s)
  249.     else:
  250.         print 'Type name for personalised layout'
  251.     s.clean
  252.  
  253.